<template>
  <el-descriptions border direction="vertical" :column="9">
    <el-descriptions-item label="月份">{{ month }}月</el-descriptions-item>
    <el-descriptions-item v-for="value, key in DetailKey" :key="key" :label="value">
      {{ detailValue[key] }}
    </el-descriptions-item>
    <el-descriptions-item label="操作">
      <el-button type="primary" plain size="small" @click="handleToException">查看详情</el-button>
    </el-descriptions-item>
    <el-descriptions-item label="考勤状态">
      <el-tag :type="detailState.type" size="small">{{ detailState.text }}</el-tag>
    </el-descriptions-item>
  </el-descriptions>
  <el-calendar v-model="date">
    <template #header>
      <el-button type="primary" @click="handlePutTime">在线签到</el-button>
      <el-space>
        <el-button plain>{{ year }}年</el-button>
        <el-select v-model="month" @change="handleChange">
          <el-option v-for="item in 12" :key="item" :value="item" :label="item + '月'" />
        </el-select>
      </el-space>
    </template>
    <template #dateCell="{ data }">
      <div>{{ renderDate(data.day) }}</div>
      <div class="show-time">{{ renderTime(data.day) }}</div>
    </template>
  </el-calendar>
</template>

<script setup lang="ts">
import { ref, reactive, computed, watchEffect } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from '@/store'
import { ElMessage } from 'element-plus'
import { toZero } from '@/utils/common';

const router = useRouter()
const store = useStore()

// 打卡信息
const signsInfos = computed(() => store.state.signs.infos)
// 用户信息
const usersInfos = computed(() => store.state.users.infos)
// 当前时间日期对象
const date = ref(new Date());
// 当前年
const year = date.value.getFullYear();
// 当前月分
const month = ref(date.value.getMonth() + 1);

/**
 * 统计考勤枚举
 */
enum DetailKey {
  normal = '正常出勤',
  absent = '旷工',
  miss = '漏打卡',
  late = '迟到',
  early = '早退',
  lateAndEarly = '迟到并早退'
}
/**
 * 统计考勤状态时长
 */
const detailValue = reactive({
  normal: 0,
  absent: 0,
  miss: 0,
  late: 0,
  early: 0,
  lateAndEarly: 0
})

// 考勤状态
const detailState = reactive({
  type: 'success' as 'success' | 'danger',
  text: '正常' as '正常' | '异常'
})

/**
 * 监听函数里所使用到的依赖，只要有一个发生过更改，就重新执行一次
 */
watchEffect((reset) => {
  // 拿到当月考勤信息
  const detailMonth = ((signsInfos.value.detail as { [index: string]: unknown })[toZero(month.value)] as { [index: string]: unknown })
  // 遍历当月对象--统计当月考勤时长情况
  for (const attr in detailMonth) {
    switch (detailMonth[attr]) {
      case DetailKey.normal:
        detailValue.normal++
        break
      case DetailKey.absent:
        detailValue.absent++
        break
      case DetailKey.miss:
        detailValue.miss++
        break
      case DetailKey.late:
        detailValue.late++
        break
      case DetailKey.early:
        detailValue.early++
        break
      case DetailKey.lateAndEarly:
        detailValue.lateAndEarly++
        break
    }
  }

  // 处理考勤状态
  for (const attr in detailValue) {
    if (attr !== 'normal' && detailValue[attr as keyof typeof detailValue] !== 0) {
      detailState.type = 'danger'
      detailState.text = '异常'
    }
  }

  // 执行上面逻辑之前--先重置一下部分属性值
  reset(() => {
    detailState.type = 'success'
    detailState.text = '正常'

    for (const attr in detailValue) {
      detailValue[attr as keyof typeof detailValue] = 0
    }

  })

})

/**
 * 下拉事件--选择月份
 */
const handleChange = () => {
  date.value = new Date(`${year}.${month.value}`);
};

/**
 * 页面跳转--查看详情
 */
const handleToException = () => {
  router.push({
    path: '/exception',
    query: { month: month.value }
  });
}
/**
 * 日期--返回当月多少号
 */
const renderDate = (day: string) => {
  return day.split('-')[2];
}

/**
 * 根据当前日期，从考勤打开信息里筛选出传入的日期--对应的打卡时间信息
 * @param day 日期: 2023-06-26
 */
const renderTime = (day: string) => {
  // 结构 日期年月日 重命名
  const [, month, date] = day.split('-');
  const ret = ((signsInfos.value.time as { [index: string]: unknown })[month] as { [index: string]: unknown })[date];
  if (Array.isArray(ret)) {
    return ret.join('-');
  }
}

/**
 * 在线签到 -- 更新打卡信息
 */
const handlePutTime = () => {
  store.dispatch('signs/putTime', { userid: usersInfos.value._id }).then((res) => {
    if (res.data.errcode === 0) {
      store.commit('signs/updateInfos', res.data.infos);
      ElMessage.success('签到成功')
    }
  })
}
</script>

<style scoped lang="scss">
.el-descriptions {
  margin: 10px;
}

.el-select {
  width: 80px;
}

.show-time {
  text-align: center;
  line-height: 40px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
</style>